﻿using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UMLSynthesizer.CSharpAnalyzer.Elements;
using UMLSynthesizer.CSharpAnalyzer.Helpers;
using UMLSynthesizer.LangAnalyzer.Elements;
using UMLSynthesizer.LangAnalyzer.Expressions;
using UMLSynthesizer.LangAnalyzer.Statements;

namespace UMLSynthesizer.CSharpAnalyzer.Expressions
{
    class CSharpInvocationExpression : CSharpExpressionBase, IInvocationExpression
    {
        #region Fields
        
        CSharpExpressionHelper argExprHelper;
        CSharpExpressionHelper exprHelper;

        #endregion

        #region Constructor

        public CSharpInvocationExpression(InvocationExpressionSyntax expressionNode,
            IExpressionBase parentExpression,
            IStatementBase parentStatement)
            : base(expressionNode, parentExpression, parentStatement, ExpressionKinds.Invocation)
        {
            argExprHelper = new CSharpExpressionHelper(this, parentStatement,
                () => from arg in expressionNode.ArgumentList.Arguments
                      select arg.Expression);
            exprHelper = new CSharpExpressionHelper(this, parentStatement,
                () => expressionNode.Expression);
        }

        #endregion

        #region Properties

        public IEnumerable<IExpressionBase> ArgumentList { get { return argExprHelper.Expressions; } }
        public IExpressionBase Expression { get { return exprHelper.Expression; } }
        public IMethodElement InvokedMethod
        {
            get
            {
                var element = (ProjectOwner as CSharpProjectElement).GetElementReference(
                    (expressionNode as InvocationExpressionSyntax).Expression);

                if(element != null && element.Kind == ElementKinds.Method)
                    return element as IMethodElement;
                return null;
            }
        }

        #endregion
    }
}
